package com.qc31.monitorlib.ui.carmonitor

import androidx.recyclerview.widget.LinearLayoutManager
import com.qc31.baselib.base.BaseRecycleActivity
import com.qc31.baselib.rx.LiveDataBus
import com.qc31.baselib.utils.ToolbarHelper
import com.qc31.baselib.utils.startActivityTo
import com.qc31.comlib.Keys
import com.qc31.comlib.TimeUtil
import com.qc31.comlib.entity.HistoryVideo
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.ActivityToolbarListBinding

/**
 *@Author Jasper
 *@Time   2020/9/29
 *@Desc   历史视频列表
 */
class HistoryVideoInfoActivity :
    BaseRecycleActivity<ActivityToolbarListBinding>(ActivityToolbarListBinding::inflate) {

    override fun initView() {
        super.initView()
        val isTmn: Boolean = intent.getBooleanExtra("isTmn", true)
        val carId: String? = intent.getStringExtra(Keys.INTENT_CAR_ID)
        val title = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        ToolbarHelper.setToolbar(this, title)

        LiveDataBus.get().with("key_history_video")
            .observeStick(this) {
                val result = it as MutableList<HistoryVideo.Result>

                result.forEach { item ->
                    item.duration = TimeUtil.instance.getDiffAllTime(
                        "20${item.startTime}",
                        "20${item.endTime}"
                    )
                }

                mRecyclerVB.recyclerView.layoutManager = LinearLayoutManager(this)
                mRecyclerVB.recyclerView.adapter = HistoryVideoInfoAdapter(isTmn).apply {
                    setNewInstance(result)
                    setOnItemChildClickListener { adapter, view, position ->
                        if (carId.isNullOrEmpty()) return@setOnItemChildClickListener
                        val item = data[position]
                        if (isTmn)
                            startActivityTo<TmnVideoActivity>(this@HistoryVideoInfoActivity) {
                                putExtra(Keys.INTENT_CAR_ID, carId)
                                putExtra(Keys.INTENT_TITLE, title)
                                putExtra(Keys.INTENT_DATA, item)
                            }
                        else
                            startActivityTo<PlatFormVideoActivity>(this@HistoryVideoInfoActivity) {
                                putExtra(Keys.INTENT_CAR_ID, carId)
                                putExtra(Keys.INTENT_TITLE, "$title  ${getString(R.string.desc_video_pass)+item.channel}")
                                putExtra(Keys.INTENT_DATA, item.fileName)
                                putExtra(Keys.INTENT_OTHER, item.duration)
                            }
                    }
                }

            }

    }

    override fun setListener() {

    }


}